import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/components/index'
import Order from '@/components/order'
import My from '@/components/my'
import NotFound from '@/components/notFound'

Vue.use(Router)

let routes = [{
    path: '/',
    redirect: { name: "index" }
  },
  {
    path: '/index',
    name: 'index',
    component: Index
  },
  {
    path: '/order',
    name: 'order',
    component: Order
  },
  {
    path: '/my',
    name: 'my',
    component: My
  },
  {
    path: '*',
    component: NotFound
  }
]




if (localStorage.getItem('AppToken')) {
  stores.commit(types.LOGIN, window.localStorage.getItem('AppToken'))
}

const router = new Router({
  // base: '/min-static/',
  // mode:'history',
  routes
});
router.beforeEach((to, from, next) => {
  if (to.matched.some(r => r.meta.requiresAuth)) {
    // console.log(stores.state.mutations.token)
    if (stores.state.mutations.token) {
      next();
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next();
  }
})

export default router
